<?php
class SubjectController extends BaseController
{
    public function __construct($uri)
    {
        parent::__construct($uri);
    }

    private function addUri($id)
    {
        $this->checkLogin();
        $m = new MovieModel();
        $r = array();
        $r['movie'] = $m->get($id);
        $r['providers'] = $m->getProviders();
        $this->output['tpl'] = 'subject/uri/add';
        $this->output['data'] = $r;
        return $this->output;
    }

    private function addUriSubmit($id)
    {
        if(empty($this->input['torrent_filename'])) {
            throw ControllerException();
        }
        $user = $this->checkLogin();
        $data = $this->input;
        $data['movie_id'] = $id;
        $data['user_id'] = $user['user_id'];
        $data['torrent_id'] = substr($data['torrent_filename'], 0, 32);
        switch ($this->input['download']) {
            case 'free_genuine_download' :
                $data['is_free'] = 1;
                $data['is_genuine'] = 1;
                $data['is_direct_download'] = 0;
                break;
            case 'chargeable_genuine_download' :
                $data['is_free'] = 0;
                $data['is_genuine'] = 1;
                break;
            case 'free_direct_download' :
                $data['is_free'] = 1;
                $data['is_direct_download'] = 1;
                break;
        }
        $m = new MovieModel();
        $r = $m->addUri($data);
        $this->output['tpl'] = 'subject/uri/add_submit';
        $this->output['data'] = $data;
        return $this->output;
    }
    
    private function checkLogin()
    {
        if(isset($_COOKIE['access_token'])) {
            $m = new UserModel();
            //todo catch
            $token = $m->decryptToken($_COOKIE['access_token']); //todo move to __construct
            //$user = $m->get($token['user_id']);
            return $token;
        }
        $this->output['http']['status_code'] = 302;
        $this->output['http']['uri'] = 'http://www.doubanxia.com/accounts/login';
        return $this->output;
    }
    /**
     * id从uri中获得
     * web server判断静态文件是否存在，如果不存在进入php
     * 触发请求豆瓣api获得名称和评分，保存到数据库
     * 静态化
     * 用户上传种子后，bt静态化，异步(todo)：重新请求豆瓣api，更新名字和评分
     * 登录按钮用js检测cookie，pt用ajax
     */
    private function get($id) {
        $m = new MovieModel();
        $r = array();
        $r['movie'] = $m->get($id);
        $r['movie_uri'] = $m->getUri($id);
        $providers = $m->getProviders();
        foreach($providers as $v) {
            $r['providers'][$v['id']] = $v['name'];
        }
        $r['resolution'] = array_flip(MovieUriDb::$resolution);
        $this->output['tpl'] = 'subject/get';
        $this->output['data'] = $r;
        return $this->output;
    }

    private function index()
    {
        $this->output['http']['status_code'] = 302;
        $this->output['http']['uri'] = '/';
        return $this->output;
    }

    public function route()
    {
        if(1 === preg_match('/^\/subject\/$/', $this->uri)) {
            switch($this->http_method) {
                case 'get' :
                    return $this->index();
                    break;
            }
        }

        if(1 === preg_match('/^\/subject\/\d+$/', $this->uri)) {
            switch($this->http_method) {
                case 'get' :
                    $this->output['http']['status_code'] = 301;
                    $this->output['http']['uri'] = $this->uri . '/';
                    return $this->output;
                    break;
            }
        }

        if(1 === preg_match('/^\/subject\/\d+\/$/', $this->uri)) {
            $tmp = explode('/', $this->uri);
            switch($this->http_method) {
                case 'get' :
                    return $this->get($tmp[2]);
                    break;
            }
        }

        if(1 === preg_match('/^\/subject\/\d+\/uri\/add$/', $this->uri)) {
            $tmp = explode('/', $this->uri);
            switch($this->http_method) {
                case 'get' :
                    return $this->addUri($tmp[2]);
                    break;
                case 'post' :
                    return $this->addUriSubmit($tmp[2]);
                    break;
            }
        }
        throw new Exception(); //todo ControllerException();
    }
}
?>
